package com.aaa.product.mq;



import com.aaa.product.dao.TbOrdInfMapper;
import com.aaa.product.entity.SeckillStatus;
import com.aaa.product.entity.TbActProduct;
import com.aaa.product.entity.TbOrdInf;
import com.aaa.product.service.TbActProductService;
import com.aaa.product.service.impl.SeckKillServiceImpl;
import com.aaa.product.task.MultiThreadingCreateOrder;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Component
@Configuration
@SuppressWarnings("ALL")
public class SeckillMessageLister {

    //交换机名称
    public static final String ITEM_TOPIC_EXCHANGE = "xwl_exchange";
    //队列名称
    public static final String ITEM_QUEUE = "xwl_queue";
    private RedisTemplate re;
    private TbActProductService tbActProductService;

    private TbOrdInfMapper tbOrdInfMapper;

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private TbActProductService sr;
    @Autowired
    private TbOrdInfMapper oo;
    @Resource
    MultiThreadingCreateOrder multiThreadingCreateOrder;

    @Resource
    SeckKillServiceImpl seckKillService;
    @PostConstruct
    public void init() {
        this.re = redisTemplate;
        this.tbOrdInfMapper = oo;
        this.tbActProductService = sr;
    }
    /**
     * 监听主队列~
     *
     * @param message
     * @param map
     * @param channel
     * @throws InterruptedException
     * @throws IOException
     */
    @RabbitListener(queues = "xwl_queue")
    public void sendMiss(String message, @Headers Map<String, Object> map, Channel channel) throws InterruptedException, IOException {
        TbOrdInf tbOrdInf = JSON.toJavaObject(JSON.parseObject(message), TbOrdInf.class);
        System.out.println(tbOrdInf);

//         创建排队对象
        SeckillStatus seckillStatus = new SeckillStatus(tbOrdInf,tbOrdInf.getAccountId(),1,tbOrdInf.getTid(),null);
        // list 是队列类型, 用户抢单排队
        redisTemplate.boundListOps("SeckillOrderQueue").leftPush(seckillStatus);
        // 一个用户只能有一个排队状态 ,抢单
        redisTemplate.boundHashOps("UserQueueStatus").put(tbOrdInf.getAccountId(),seckillStatus);

        multiThreadingCreateOrder.createOrder();

    }
    /**
     * 监听死信队列-即推送抢单失败
     *
     * @param message
     * @param map
     * @param channel
     * @throws InterruptedException
     * @throws IOException
     */
    @RabbitListener(queues = "deal_queue")
    public static void sendMiss2(Message message, @Headers Map<String, Object> map, Channel channel) throws InterruptedException, IOException {
        String msg = new String(message.getBody(), "UTF-8");
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        System.out.println(msg + "商品已被抢空~下次再来");
    }
}
